package com.aranandroid.customview.squareview.base

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import androidx.annotation.Nullable
import com.aranandroid.customview.R
import com.aranandroid.customview.utils.DrawableBuilder

open class SquareNormalView(
    @Nullable context: Context?,
    @Nullable attrs: AttributeSet?
) {
    companion object{
        val DEFZERO = -1F
    }
    private var corner: Float = 0F

    var cornerlt: Float
    var cornerlb: Float
    var cornerrt: Float
    var cornerrb: Float

    var dashwidth: Float = 0F
    var dashgap: Float = 0F
    var linecolor: Int = 0
    var fillcolor: Int = 0
    var linewidth: Float = 0F

// 文字
    var textColor: Int = 0
// 图片
    var topDraw:Drawable? = null
    var leftDraw:Drawable? = null
    var bottomDraw:Drawable? = null
    var rightDraw:Drawable? = null


    fun setCorner(value:Float) {
        corner = value
        cornerlt = 0F
        cornerlb = 0F
        cornerrt = 0F
        cornerrb = 0F

    }


    init {
        val obtainStyledAttributes =
            context!!.obtainStyledAttributes(attrs, R.styleable.View)
        //圆角
        corner =
            obtainStyledAttributes.getDimension(R.styleable.View_corner, DEFZERO)
        cornerlt =
            obtainStyledAttributes.getDimension(R.styleable.View_corner_left_top, DEFZERO)
        cornerlb = obtainStyledAttributes.getDimension(
            R.styleable.View_corner_left_bottom,
            DEFZERO
        )
        cornerrt = obtainStyledAttributes.getDimension(
            R.styleable.View_corner_right_top,
            DEFZERO
        )
        cornerrb = obtainStyledAttributes.getDimension(
            R.styleable.View_corner_right_bottom,
            DEFZERO
        )
        //线宽
        linewidth =
            obtainStyledAttributes.getDimension(R.styleable.View_line_width, DEFZERO)
        //虚线宽度
        dashwidth =
            obtainStyledAttributes.getDimension(R.styleable.View_dash_width, DEFZERO)
        // 虚线间距
        dashgap = obtainStyledAttributes.getDimension(R.styleable.View_dash_gap, 2F)
        // 线颜色
        linecolor = obtainStyledAttributes.getColor(
            R.styleable.View_line_color,
            Color.TRANSPARENT
        )
        // 填充颜色
        fillcolor = obtainStyledAttributes.getColor(
            R.styleable.View_background_color,
            Color.TRANSPARENT
        )
        // 字体颜色
        textColor = obtainStyledAttributes.getColor(
            R.styleable.View_text_color,
            0
        )
        // draw 图片
        topDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_drawable_top)
        leftDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_drawable_left)
        rightDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_drawable_right)
        bottomDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_drawable_bottom)

    }

    fun getCorner():Float{
        return corner
    }

    fun getBackGround(): Drawable {
        var drawableBuilder = DrawableBuilder().corner()
        if (DEFZERO != corner) {
            drawableBuilder = drawableBuilder.corner(
                floatArrayOf(
                    corner, corner, corner, corner,
                    corner, corner, corner, corner
                )
            )
        }

        if (DEFZERO != cornerlt || DEFZERO != cornerlb || DEFZERO != cornerrt || DEFZERO != cornerrb) {
            drawableBuilder =
                drawableBuilder.corner(
                    floatArrayOf(
                        cornerlt, cornerlt, cornerrt, cornerrt,
                         cornerrb, cornerrb,cornerlb, cornerlb
                    )
                )
        }

        if (DEFZERO != linewidth) {
            drawableBuilder = drawableBuilder.line().lineWidth(linewidth).lineColor(linecolor)
        }

        if (DEFZERO != dashwidth) {
            drawableBuilder = drawableBuilder.dash().dashWidth(dashwidth).dashGap(dashgap)
        }

        if (fillcolor != 0) {
            drawableBuilder = drawableBuilder.fill(fillcolor)
        }

        return drawableBuilder.build()
    }




}